﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Truextend.CoffeShop.Presenter.Orders;
using System.Data;
using Microsoft.SharePoint;

namespace Truextend.CoffeShop.Orders
{
    public partial class OrdersUserControl : UserControl , IOrdersMenu
    {
        OrdersMenuPresenter presenter = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            presenter = new OrdersMenuPresenter(this);
            if(!IsPostBack){
                presenter.FillOrdersInProgress("INP");
            }
        }

        protected void OrdersGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            string orderSelected = OrdersGridView.DataKeys[OrdersGridView.SelectedIndex].Value.ToString();

            GridViewRow row = OrdersGridView.SelectedRow;

            LinkButton link = row.FindControl("LinkJoin") as LinkButton;

            if(link.Text == "Join")
            {
                Response.Redirect("/_layouts/Truextend.CoffeShop/ParticipateInOrder.aspx?orderId="+orderSelected);
            }
            else
            {
                Response.Redirect("/_layouts/Truextend.CoffeShop/EditOrder.aspx?orderId=" + orderSelected);
            }
            
        }

        #region  Implements IOrdersMenu

        public System.Data.DataTable orders
        {
            get
            {
                DataTable data_table = new DataTable();

                data_table.Columns.Add("OrderID");
                data_table.Columns.Add("Title");
                data_table.Columns.Add("Restaurant");
                data_table.Columns.Add("StartDate");
                data_table.Columns.Add("EndDate");

                for (int i = 0; i < OrdersGridView.Rows.Count; i++)
                {
                    GridViewRow row = OrdersGridView.Rows[i];

                    DataRow data_row = data_table.NewRow();
                    data_row["OrderID"] = this.OrdersGridView.DataKeys[i].Value.ToString();
                    data_row["Title"] = (row.FindControl("LblTitle") as Label).Text;
                    data_row["Restaurant"] = (row.FindControl("LblRestaurant") as Label).Text;
                    data_row["StartDate"] = (row.FindControl("LblStartDate") as Label).Text;
                    data_row["EndDate"] = (row.FindControl("LblEndDate") as Label).Text;

                    data_table.Rows.Add(data_row);
                }

                return data_table;
            }
            set
            {
                OrdersGridView.DataSource = value;
                OrdersGridView.Columns[0].Visible = false;
                OrdersGridView.DataBind();
            }
        }

        public int UserId
        {
            get
            {
                return SPContext.Current.Web.CurrentUser.ID;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        protected void LinkBtnRefresh_Click(object sender, EventArgs e)
        {
            presenter.FillOrdersInProgress("INP");
        }

        protected void OrdersGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int orderId = int.Parse(OrdersGridView.DataKeys[e.RowIndex].Value.ToString());
            presenter.CancelOrder(orderId);
            presenter.FillOrdersInProgress("INP");
        }

        protected void LinkBtnNewOrder_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/CoffeeShop/SitePages/CreateANewGrupalOrder.aspx");
        }
        
    }
}
